home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / jpeg / sources / jpegdata.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-15  |  39.0 KB  |  945 lines

  1. /*
  2.  * jpegdata.h
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file defines shared data structures for the various JPEG modules.
  9.  */
  10.  
  11.  
  12. /*
  13.  * You might need to change some of the following declarations if you are
  14.  * using the JPEG software within a surrounding application program
  15.  * or porting it to an unusual system.
  16.  */
  17.  
  18.  
  19. /* If the source or destination of image data is not to be stdio streams,
  20.  * these types may need work.  You can replace them with some kind of
  21.  * pointer or indicator that is useful to you, or just ignore 'em.
  22.  * Note that the user interface and the various jrdxxx/jwrxxx modules
  23.  * will also need work for non-stdio input/output.
  24.  */
  25.  
  26. typedef FILE * JFILEREF;    /* source or dest of JPEG-compressed data */
  27.  
  28. typedef FILE * IFILEREF;    /* source or dest of non-JPEG image data */
  29.  
  30.  
  31. /* These defines are used in all function definitions and extern declarations.
  32.  * You could modify them if you need to change function linkage conventions,
  33.  * as is shown below for use with C++.  Another application would be to make
  34.  * all functions global for use with code profilers that require it.
  35.  * NOTE: the C++ test does the right thing if you are reading this include
  36.  * file in a C++ application to link to JPEG code that's been compiled with a
  37.  * regular C compiler.  I'm not sure it works if you try to compile the JPEG
  38.  * code with C++.
  39.  */
  40.  
  41. #define METHODDEF static    /* a function called through method pointers */
  42. #define LOCAL      static    /* a function used only in its module */
  43. #define GLOBAL            /* a function referenced thru EXTERNs */
  44. #ifdef __cplusplus
  45. #define EXTERN      extern "C"    /* a reference to a GLOBAL function */
  46. #else
  47. #define EXTERN      extern    /* a reference to a GLOBAL function */
  48. #endif
  49.  
  50.  
  51. /* Here is the pseudo-keyword for declaring pointers that must be "far"
  52.  * on 80x86 machines.  Most of the specialized coding for 80x86 is handled
  53.  * by just saying "FAR *" where such a pointer is needed.  In a few places
  54.  * explicit coding is needed; see uses of the NEED_FAR_POINTERS symbol.
  55.  */
  56.  
  57. #ifdef NEED_FAR_POINTERS
  58. #define FAR  far
  59. #else
  60. #define FAR
  61. #endif
  62.  
  63.  
  64.  
  65. /* The remaining declarations are not system-dependent, we hope. */
  66.  
  67.  
  68. /*
  69.  * NOTE: if you have an ancient, strict-K&R C compiler, it may choke on the
  70.  * similarly-named fields in Compress_info_struct and Decompress_info_struct.
  71.  * If this happens, you can get around it by rearranging the two structs so
  72.  * that the similarly-named fields appear first and in the same order in
  73.  * each struct.  Since such compilers are now pretty rare, we haven't done
  74.  * this in the portable code, preferring to maintain a logical ordering.
  75.  */
  76.  
  77.  
  78.  
  79. /* This macro is used to declare a "method", that is, a function pointer. */
  80. /* We want to supply prototype parameters if the compiler can cope. */
  81. /* Note that the arglist parameter must be parenthesized! */
  82.  
  83. #ifdef PROTO
  84. #define METHOD(type,methodname,arglist)  type (*methodname) arglist
  85. #else
  86. #define METHOD(type,methodname,arglist)  type (*methodname) ()
  87. #endif
  88.  
  89. /* Forward references to lists of method pointers */
  90. typedef struct External_methods_struct * external_methods_ptr;
  91. typedef struct Compress_methods_struct * compress_methods_ptr;
  92. typedef struct Decompress_methods_struct * decompress_methods_ptr;
  93.  
  94.  
  95. /* Data structures for images containing either samples or coefficients. */
  96. /* Note that the topmost (leftmost) index is always color component. */
  97. /* On 80x86 machines, the image arrays are too big for near pointers, */
  98. /* but the pointer arrays can fit in near memory. */
  99.  
  100. typedef JSAMPLE FAR *JSAMPROW;    /* ptr to one image row of pixel samples. */
  101. typedef JSAMPROW *JSAMPARRAY;    /* ptr to some rows (a 2-D sample array) */
  102. typedef JSAMPARRAY *JSAMPIMAGE;    /* a 3-D sample array: top index is color */
  103.  
  104.  
  105. #define DCTSIZE        8    /* The basic DCT block is 8x8 samples */
  106. #define DCTSIZE2    64    /* DCTSIZE squared; # of elements in a block */
  107.  
  108. typedef JCOEF JBLOCK[DCTSIZE2];    /* one block of coefficients */
  109. typedef JBLOCK FAR *JBLOCKROW;    /* pointer to one row of coefficient blocks */
  110. typedef JBLOCKROW *JBLOCKARRAY;        /* a 2-D array of coefficient blocks */
  111. typedef JBLOCKARRAY *JBLOCKIMAGE;    /* a 3-D array of coefficient blocks */
  112.  
  113. typedef JCOEF FAR *JCOEFPTR;    /* useful in a couple of places */
  114.  
  115.  
  116. /* The input and output data of the DCT transform subroutines are of
  117.  * the following type, which need not be the same as JCOEF.
  118.  * For example, on a machine with fast floating point, it might make sense
  119.  * to recode the DCT routines to use floating point; then DCTELEM would be
  120.  * 'float' or 'double'.
  121.  */
  122.  
  123. typedef JCOEF DCTELEM;
  124. typedef DCTELEM DCTBLOCK[DCTSIZE2];
  125.  
  126.  
  127. /* Types for JPEG compression parameters and working tables. */
  128.  
  129.  
  130. typedef enum {            /* defines known color spaces */
  131.     CS_UNKNOWN,        /* error/unspecified */
  132.     CS_GRAYSCALE,        /* monochrome (only 1 component) */
  133.     CS_RGB,            /* red/green/blue */
  134.     CS_YCbCr,        /* Y/Cb/Cr (also known as YUV) */
  135.     CS_YIQ,            /* Y/I/Q */
  136.     CS_CMYK            /* C/M/Y/K */
  137. } COLOR_SPACE;
  138.  
  139.  
  140. typedef struct {        /* Basic info about one component */
  141.   /* These values are fixed over the whole image */
  142.   /* For compression, they must be supplied by the user interface; */
  143.   /* for decompression, they are read from the SOF marker. */
  144.     short component_id;    /* identifier for this component (0..255) */
  145.     short component_index;    /* its index in SOF or cinfo->comp_info[] */
  146.     short h_samp_factor;    /* horizontal sampling factor (1..4) */
  147.     short v_samp_factor;    /* vertical sampling factor (1..4) */
  148.     short quant_tbl_no;    /* quantization table selector (0..3) */
  149.   /* These values may vary between scans */
  150.   /* For compression, they must be supplied by the user interface; */
  151.   /* for decompression, they are read from the SOS marker. */
  152.     short dc_tbl_no;    /* DC entropy table selector (0..3) */
  153.     short ac_tbl_no;    /* AC entropy table selector (0..3) */
  154.   /* These values are computed during compression or decompression startup */
  155.     long true_comp_width;    /* component's image width in samples */
  156.     long true_comp_height;    /* component's image height in samples */
  157.     /* the above are the logical dimensions of the downsampled image */
  158.   /* These values are computed before starting a scan of the component */
  159.     short MCU_width;    /* number of blocks per MCU, horizontally */
  160.     short MCU_height;    /* number of blocks per MCU, vertically */
  161.     short MCU_blocks;    /* MCU_width * MCU_height */
  162.     long downsampled_width;    /* image width in samples, after expansion */
  163.     long downsampled_height; /* image height in samples, after expansion */
  164.     /* the above are the true_comp_xxx values rounded up to multiples of */
  165.     /* the MCU dimensions; these are the working dimensions of the array */
  166.     /* as it is passed through the DCT or IDCT step.  NOTE: these values */
  167.     /* differ depending on whether the component is interleaved or not!! */
  168. } jpeg_component_info;
  169.  
  170.  
  171. /* DCT coefficient quantization tables.
  172.  * For 8-bit precision, 'INT16' should be good enough for quantization values;
  173.  * for more precision, we go for the full 16 bits.  'INT16' provides a useful
  174.  * speedup on many machines (multiplication & division of JCOEFs by
  175.  * quantization values is a significant chunk of the runtime).
  176.  * Note: the values in a QUANT_TBL are always given in zigzag order.
  177.  */
  178. #ifdef EIGHT_BIT_SAMPLES
  179. typedef INT16 QUANT_VAL;    /* element of a quantization table */
  180. #else
  181. typedef UINT16 QUANT_VAL;    /* element of a quantization table */
  182. #endif
  183. typedef QUANT_VAL QUANT_TBL[DCTSIZE2];    /* A quantization table */
  184. typedef QUANT_VAL * QUANT_TBL_PTR;    /* pointer to same */
  185.  
  186.  
  187. typedef struct {        /* A Huffman coding table */
  188.   /* These two fields directly represent the contents of a JPEG DHT marker */
  189.     UINT8 bits[17];        /* bits[k] = # of symbols with codes of */
  190.                 /* length k bits; bits[0] is unused */
  191.     UINT8 huffval[256];    /* The symbols, in order of incr code length */
  192.   /* This field is used only during compression.  It's initialized FALSE when
  193.    * the table is created, and set TRUE when it's been output to the file.
  194.    */
  195.     boolean sent_table;    /* TRUE when table has been output */
  196.   /* The remaining fields are computed from the above to allow more efficient
  197.    * coding and decoding.  These fields should be considered private to the
  198.    * Huffman compression & decompression modules.
  199.    */
  200.     /* encoding tables: */
  201.     UINT16 ehufco[256];    /* code for each symbol */
  202.     char ehufsi[256];    /* length of code for each symbol */
  203.     /* decoding tables: (element [0] of each array is unused) */
  204.     UINT16 mincode[17];    /* smallest code of length k */
  205.     INT32 maxcode[18];    /* largest code of length k (-1 if none) */
  206.     /* (maxcode[17] is a sentinel to ensure huff_DECODE terminates) */
  207.     short valptr[17];    /* huffval[] index of 1st symbol of length k */
  208. } HUFF_TBL;
  209.  
  210.  
  211. #define NUM_QUANT_TBLS      4    /* quantization tables are numbered 0..3 */
  212. #define NUM_HUFF_TBLS       4    /* Huffman tables are numbered 0..3 */
  213. #define NUM_ARITH_TBLS      16    /* arith-coding tables are numbered 0..15 */
  214. #define MAX_COMPS_IN_SCAN   4    /* JPEG limit on # of components in one scan */
  215. #define MAX_SAMP_FACTOR     4    /* JPEG limit on sampling factors */
  216. #define MAX_BLOCKS_IN_MCU   10    /* JPEG limit on # of blocks in an MCU */
  217.  
  218.  
  219. /* Working data for compression */
  220.  
  221. struct Compress_info_struct {
  222. /*
  223.  * All of these fields shall be established by the user interface before
  224.  * calling jpeg_compress, or by the input_init or c_ui_method_selection
  225.  * methods.
  226.  * Most parameters can be set to reasonable defaults by j_c_defaults.
  227.  * Note that the UI must supply the storage for the main methods struct,
  228.  * though it sets only a few of the methods there.
  229.  */
  230.     compress_methods_ptr methods; /* Points to list of methods to use */
  231.  
  232.     external_methods_ptr emethods; /* Points to list of methods to use */
  233.  
  234.     IFILEREF input_file;    /* tells input routines where to read image */
  235.     JFILEREF output_file;    /* tells output routines where to write JPEG */
  236.  
  237.     long image_width;    /* input image width */
  238.     long image_height;    /* input image height */
  239.     short input_components;    /* # of color components in input image */
  240.  
  241.     short data_precision;    /* bits of precision in image data */
  242.  
  243.     COLOR_SPACE in_color_space; /* colorspace of input file */
  244.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  245.  
  246.     double input_gamma;    /* image gamma of input file */
  247.  
  248.     boolean write_JFIF_header; /* should a JFIF marker be written? */
  249.     /* These three values are not used by the JPEG code, only copied */
  250.     /* into the JFIF APP0 marker.  density_unit can be 0 for unknown, */
  251.     /* 1 for dots/inch, or 2 for dots/cm.  Note that the pixel aspect */
  252.     /* ratio is defined by X_density/Y_density even when density_unit=0. */
  253.     UINT8 density_unit;    /* JFIF code for pixel size units */
  254.     UINT16 X_density;    /* Horizontal pixel density */
  255.     UINT16 Y_density;    /* Vertical pixel density */
  256.  
  257.     short num_components;    /* # of color components in JPEG image */
  258.     jpeg_component_info * comp_info;
  259.     /* comp_info[i] describes component that appears i'th in SOF */
  260.  
  261.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  262.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  263.  
  264.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  265.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  266.     /* ptrs to Huffman coding tables, or NULL if not defined */
  267.  
  268.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arithmetic-coding tables */
  269.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arithmetic-coding tables */
  270.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arithmetic-coding tables */
  271.  
  272.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  273.     boolean interleave;    /* TRUE=interleaved output, FALSE=not */
  274.     boolean optimize_coding; /* TRUE=optimize entropy encoding parms */
  275.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  276.     int smoothing_factor;    /* 1..100, or 0 for no input smoothing */
  277.  
  278.     /* The restart interval can be specified in absolute MCUs by setting
  279.      * restart_interval, or in MCU rows by setting restart_in_rows
  280.      * (in which case the correct restart_interval will be figured
  281.      * for each scan).
  282.      */
  283.     UINT16 restart_interval;/* MCUs per restart interval, or 0 for no restart */
  284.     int restart_in_rows;    /* if > 0, MCU rows per restart interval */
  285.  
  286. /*
  287.  * These fields are computed during jpeg_compress startup
  288.  */
  289.     short max_h_samp_factor; /* largest h_samp_factor */
  290.     short max_v_samp_factor; /* largest v_samp_factor */
  291.  
  292. /*
  293.  * These fields may be useful for progress monitoring
  294.  */
  295.  
  296.     int total_passes;    /* number of passes expected */
  297.     int completed_passes;    /* number of passes completed so far */
  298.  
  299. /*
  300.  * These fields are valid during any one scan
  301.  */
  302.     short comps_in_scan;    /* # of JPEG components output this time */
  303.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  304.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  305.  
  306.     long MCUs_per_row;    /* # of MCUs across the image */
  307.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  308.  
  309.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  310.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  311.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  312.     /* i'th block in an MCU */
  313.  
  314.     /* these fields are private data for the entropy encoder */
  315.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  316.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  317.     UINT16 restarts_to_go;    /* MCUs left in this restart interval */
  318.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  319.  
  320.     U_IMAGE    *img;    /*    public common conversion lib    */
  321. };
  322.  
  323. typedef struct Compress_info_struct * compress_info_ptr;
  324.  
  325.  
  326. /* Working data for decompression */
  327.  
  328. struct Decompress_info_struct {
  329. /*
  330.  * These fields shall be established by the user interface before
  331.  * calling jpeg_decompress.
  332.  * Most parameters can be set to reasonable defaults by j_d_defaults.
  333.  * Note that the UI must supply the storage for the main methods struct,
  334.  * though it sets only a few of the methods there.
  335.  */
  336.     decompress_methods_ptr methods; /* Points to list of methods to use */
  337.  
  338.     external_methods_ptr emethods; /* Points to list of methods to use */
  339.  
  340.     JFILEREF input_file;    /* tells input routines where to read JPEG */
  341.     IFILEREF output_file;    /* tells output routines where to write image */
  342.  
  343.     /* these can be set at d_ui_method_selection time: */
  344.  
  345.     COLOR_SPACE out_color_space; /* colorspace of output */
  346.  
  347.     double output_gamma;    /* image gamma wanted in output */
  348.  
  349.     boolean quantize_colors; /* T if output is a colormapped format */
  350.     /* the following are ignored if not quantize_colors: */
  351.     boolean two_pass_quantize;    /* use two-pass color quantization? */
  352.     boolean use_dithering;        /* want color dithering? */
  353.     int desired_number_of_colors;    /* max number of colors to use */
  354.  
  355.     boolean do_block_smoothing; /* T = apply cross-block smoothing */
  356.     boolean do_pixel_smoothing; /* T = apply post-upsampling smoothing */
  357.  
  358. /*
  359.  * These fields are used for efficient buffering of data between read_jpeg_data
  360.  * and the entropy decoding object.  By using a shared buffer, we avoid copying
  361.  * data and eliminate the need for an "unget" operation at the end of a scan.
  362.  * The actual source of the data is known only to read_jpeg_data; see the
  363.  * JGETC macro, below.
  364.  * Note: the user interface is expected to allocate the input_buffer and
  365.  * initialize bytes_in_buffer to 0.  Also, for JFIF/raw-JPEG input, the UI
  366.  * actually supplies the read_jpeg_data method.  This is all handled by
  367.  * j_d_defaults in a typical implementation.
  368.  */
  369.     char * input_buffer;    /* start of buffer (private to input code) */
  370.     char * next_input_byte;    /* => next byte to read from buffer */
  371.     int bytes_in_buffer;    /* # of bytes remaining in buffer */
  372.  
  373. /*
  374.  * These fields are set by read_file_header or read_scan_header
  375.  */
  376.     long image_width;    /* overall image width */
  377.     long image_height;    /* overall image height */
  378.  
  379.     short data_precision;    /* bits of precision in image data */
  380.  
  381.     COLOR_SPACE jpeg_color_space; /* colorspace of JPEG file */
  382.  
  383.         /* These three values are not used by the JPEG code, merely copied */
  384.     /* from the JFIF APP0 marker (if any). */
  385.     UINT8 density_unit;    /* JFIF code for pixel size units */
  386.     UINT16 X_density;    /* Horizontal pixel density */
  387.     UINT16 Y_density;    /* Vertical pixel density */
  388.  
  389.     short num_components;    /* # of color components in JPEG image */
  390.     jpeg_component_info * comp_info;
  391.     /* comp_info[i] describes component that appears i'th in SOF */
  392.  
  393.     QUANT_TBL_PTR quant_tbl_ptrs[NUM_QUANT_TBLS];
  394.     /* ptrs to coefficient quantization tables, or NULL if not defined */
  395.  
  396.     HUFF_TBL * dc_huff_tbl_ptrs[NUM_HUFF_TBLS];
  397.     HUFF_TBL * ac_huff_tbl_ptrs[NUM_HUFF_TBLS];
  398.     /* ptrs to Huffman coding tables, or NULL if not defined */
  399.  
  400.     UINT8 arith_dc_L[NUM_ARITH_TBLS]; /* L values for DC arith-coding tables */
  401.     UINT8 arith_dc_U[NUM_ARITH_TBLS]; /* U values for DC arith-coding tables */
  402.     UINT8 arith_ac_K[NUM_ARITH_TBLS]; /* Kx values for AC arith-coding tables */
  403.  
  404.     boolean arith_code;    /* TRUE=arithmetic coding, FALSE=Huffman */
  405.     boolean CCIR601_sampling; /* TRUE=first samples are cosited */
  406.  
  407.     UINT16 restart_interval;/* MCUs per restart interval, or 0 for no restart */
  408.  
  409. /*
  410.  * These fields are computed during jpeg_decompress startup
  411.  */
  412.     short max_h_samp_factor; /* largest h_samp_factor */
  413.     short max_v_samp_factor; /* largest v_samp_factor */
  414.  
  415.     short color_out_comps;    /* # of color components output by color_convert */
  416.                 /* (need not match num_components) */
  417.     short final_out_comps;    /* # of color components sent to put_pixel_rows */
  418.     /* (1 when quantizing colors, else same as color_out_comps) */
  419.  
  420.     JSAMPLE * sample_range_limit; /* table for fast range-limiting */
  421.  
  422. /*
  423.  * When quantizing colors, the color quantizer leaves a pointer to the output
  424.  * colormap in these fields.  The colormap is valid from the time put_color_map
  425.  * is called (must be before any put_pixel_rows calls) until shutdown (more
  426.  * specifically, until free_all is called to release memory).
  427.  */
  428.     int actual_number_of_colors; /* actual number of entries */
  429.     JSAMPARRAY colormap;    /* NULL if not valid */
  430.     /* map has color_out_comps rows * actual_number_of_colors columns */
  431.  
  432. /*
  433.  * These fields may be useful for progress monitoring
  434.  */
  435.  
  436.     int total_passes;    /* number of passes expected */
  437.     int completed_passes;    /* number of passes completed so far */
  438.  
  439. /*
  440.  * These fields are valid during any one scan
  441.  */
  442.     short comps_in_scan;    /* # of JPEG components input this time */
  443.     jpeg_component_info * cur_comp_info[MAX_COMPS_IN_SCAN];
  444.     /* *cur_comp_info[i] describes component that appears i'th in SOS */
  445.  
  446.     long MCUs_per_row;    /* # of MCUs across the image */
  447.     long MCU_rows_in_scan;    /* # of MCU rows in the image */
  448.  
  449.     short blocks_in_MCU;    /* # of DCT blocks per MCU */
  450.     short MCU_membership[MAX_BLOCKS_IN_MCU];
  451.     /* MCU_membership[i] is index in cur_comp_info of component owning */
  452.     /* i'th block in an MCU */
  453.  
  454.     /* these fields are private data for the entropy encoder */
  455.     JCOEF last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each comp */
  456.     JCOEF last_dc_diff[MAX_COMPS_IN_SCAN]; /* last DC diff for each comp */
  457.     UINT16 restarts_to_go;    /* MCUs left in this restart interval */
  458.     short next_restart_num;    /* # of next RSTn marker (0..7) */
  459.  
  460.     U_IMAGE    *img;    /*    public common conversion lib    */
  461. };
  462.  
  463. typedef struct Decompress_info_struct * decompress_info_ptr;
  464.  
  465.  
  466. /* Macros for reading data from the decompression input buffer */
  467.  
  468. #ifdef CHAR_IS_UNSIGNED
  469. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  470.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  471.              (int) (*(cinfo)->next_input_byte++) )
  472. #else
  473. #define JGETC(cinfo)    ( --(cinfo)->bytes_in_buffer < 0 ? \
  474.              (*(cinfo)->methods->read_jpeg_data) (cinfo) : \
  475.              (int) (*(cinfo)->next_input_byte++) & 0xFF )
  476. #endif
  477.  
  478. #ifndef STREAM_IMAGE
  479. #define JUNGETC(ch,cinfo)  ((cinfo)->bytes_in_buffer++, \
  480.                 *(--((cinfo)->next_input_byte)) = (char) (ch))
  481. #else
  482. #define JUNGETC(ch,cinfo)    (ungetc(ch, (cinfo)->input_file))
  483. #endif
  484.  
  485. #define MIN_UNGET    4    /* may always do at least 4 JUNGETCs */
  486.  
  487.  
  488. /* A virtual image has a control block whose contents are private to the
  489.  * memory manager module (and may differ between managers).  The rest of the
  490.  * code only refers to virtual images by these pointer types, and never
  491.  * dereferences the pointer.
  492.  */
  493.  
  494. typedef struct big_sarray_control * big_sarray_ptr;
  495. typedef struct big_barray_control * big_barray_ptr;
  496.  
  497. /* Although a real ANSI C compiler can deal perfectly well with pointers to
  498.  * unspecified structures (see "incomplete types" in the spec), a few pre-ANSI
  499.  * and pseudo-ANSI compilers get confused.  To keep one of these bozos happy,
  500.  * add -DINCOMPLETE_TYPES_BROKEN to CFLAGS in your Makefile.  Then we will
  501.  * pseudo-define the structs as containing a single "dummy" field.
  502.  * The memory managers #define AM_MEMORY_MANAGER before including this file,
  503.  * so that they can make their own definitions of the structs.
  504.  */
  505.  
  506. #ifdef INCOMPLETE_TYPES_BROKEN
  507. #ifndef AM_MEMORY_MANAGER
  508. struct big_sarray_control { long dummy; };
  509. struct big_barray_control { long dummy; };
  510. #endif
  511. #endif
  512.  
  513.  
  514. /* Method types that need typedefs */
  515.  
  516. typedef METHOD(void, MCU_output_method_ptr, (compress_info_ptr cinfo,
  517.                          JBLOCK *MCU_data));
  518. typedef METHOD(void, MCU_output_caller_ptr, (compress_info_ptr cinfo,
  519.                          MCU_output_method_ptr output_method));
  520. typedef METHOD(void, downsample_ptr, (compress_info_ptr cinfo,
  521.                       int which_component,
  522.                       long input_cols, int input_rows,
  523.                       long output_cols, int output_rows,
  524.                       JSAMPARRAY above,
  525.                       JSAMPARRAY input_data,
  526.                       JSAMPARRAY below,
  527.                       JSAMPARRAY output_data));
  528. typedef METHOD(void, upsample_ptr, (decompress_info_ptr cinfo,
  529.                     int which_component,
  530.                     long input_cols, int input_rows,
  531.                     long output_cols, int output_rows,
  532.                     JSAMPARRAY above,
  533.                     JSAMPARRAY input_data,
  534.                     JSAMPARRAY below,
  535.                     JSAMPARRAY output_data));
  536. typedef METHOD(void, quantize_method_ptr, (decompress_info_ptr cinfo,
  537.                        int num_rows,
  538.                        JSAMPIMAGE input_data,
  539.                        JSAMPARRAY output_workspace));
  540. typedef METHOD(void, quantize_caller_ptr, (decompress_info_ptr cinfo,
  541.                        quantize_method_ptr quantize_method));
  542.  
  543.  
  544. /* These structs contain function pointers for the various JPEG methods. */
  545.  
  546. /* Routines to be provided by the surrounding application, rather than the
  547.  * portable JPEG code proper.  These are the same for compression and
  548.  * decompression.
  549.  */
  550.  
  551. struct External_methods_struct {
  552.     /* User interface: error exit and trace message routines */
  553.     /* NOTE: the string msgtext parameters will eventually be replaced
  554.      * by an enumerated-type code so that non-English error messages
  555.      * can be substituted easily.  This will not be done until all the
  556.      * code is in place, so that we know what messages are needed.
  557.      */
  558.     METHOD(void, error_exit, (const char *msgtext));
  559.     METHOD(void, trace_message, (const char *msgtext));
  560.  
  561.     /* Working data for error/trace facility */
  562.     /* See macros below for the usage of these variables */
  563.     int trace_level;    /* level of detail of tracing messages */
  564.     /* Use level 0 for important warning messages (nonfatal errors) */
  565.     /* Use levels 1, 2, 3 for successively more detailed trace options */
  566.  
  567.     /* For recoverable corrupt-data errors, we emit a warning message and
  568.      * keep going.  A surrounding application can check for bad data by
  569.      * seeing if num_warnings is nonzero at the end of processing.
  570.      */
  571.     long num_warnings;    /* number of corrupt-data warnings */
  572.     int first_warning_level; /* trace level for first warning */
  573.     int more_warning_level;    /* trace level for subsequent warnings */
  574.  
  575.     int message_parm[8];    /* store numeric parms for messages here */
  576.  
  577.     /* Memory management */
  578.     /* NB: alloc routines never return NULL. They exit to */
  579.     /* error_exit if not successful. */
  580.     METHOD(void *, alloc_small, (size_t sizeofobject));
  581.     METHOD(void, free_small, (void *ptr));
  582.     METHOD(void FAR *, alloc_medium, (size_t sizeofobject));
  583.     METHOD(void, free_medium, (void FAR *ptr));
  584.     METHOD(JSAMPARRAY, alloc_small_sarray, (long samplesperrow,
  585.                         long numrows));
  586.     METHOD(void, free_small_sarray, (JSAMPARRAY ptr));
  587.     METHOD(JBLOCKARRAY, alloc_small_barray, (long blocksperrow,
  588.                          long numrows));
  589.     METHOD(void, free_small_barray, (JBLOCKARRAY ptr));
  590.     METHOD(big_sarray_ptr, request_big_sarray, (long samplesperrow,
  591.                             long numrows,
  592.                             long unitheight));
  593.     METHOD(big_barray_ptr, request_big_barray, (long blocksperrow,
  594.                             long numrows,
  595.                             long unitheight));
  596.     METHOD(void, alloc_big_arrays, (long extra_small_samples,
  597.                     long extra_small_blocks,
  598.                     long extra_medium_space));
  599.     METHOD(JSAMPARRAY, access_big_sarray, (big_sarray_ptr ptr,
  600.                            long start_row,
  601.                            boolean writable));
  602.     METHOD(JBLOCKARRAY, access_big_barray, (big_barray_ptr ptr,
  603.                         long start_row,
  604.                         boolean writable));
  605.     METHOD(void, free_big_sarray, (big_sarray_ptr ptr));
  606.     METHOD(void, free_big_barray, (big_barray_ptr ptr));
  607.     METHOD(void, free_all, (void));
  608.  
  609.     long max_memory_to_use;    /* maximum amount of memory to use */
  610. };
  611.  
  612. /* Macros to simplify using the error and trace message stuff */
  613. /* The first parameter is generally cinfo->emethods */
  614.  
  615. /* Fatal errors (print message and exit)
  616. #define ERREXIT(emeth,msg)        ((*(emeth)->error_exit) (msg))
  617. #define ERREXIT1(emeth,msg,p1)        ((emeth)->message_parm[0] = (p1), \
  618.                      (*(emeth)->error_exit) (msg))
  619. #define ERREXIT2(emeth,msg,p1,p2)    ((emeth)->message_parm[0] = (p1), \
  620.                      (emeth)->message_parm[1] = (p2), \
  621.                      (*(emeth)->error_exit) (msg))
  622. #define ERREXIT3(emeth,msg,p1,p2,p3)    ((emeth)->message_parm[0] = (p1), \
  623.                      (emeth)->message_parm[1] = (p2), \
  624.                      (emeth)->message_parm[2] = (p3), \
  625.                      (*(emeth)->error_exit) (msg))
  626. #define ERREXIT4(emeth,msg,p1,p2,p3,p4) ((emeth)->message_parm[0] = (p1), \
  627.                      (emeth)->message_parm[1] = (p2), \
  628.                      (emeth)->message_parm[2] = (p3), \
  629.                      (emeth)->message_parm[3] = (p4), \
  630.                      (*(emeth)->error_exit) (msg))
  631. */
  632. #define ERREXIT(emeth,msg)    prgmerr(0, msg)
  633. #define ERREXIT1(emeth,msg,p1)    prgmerr(0, msg, p1)
  634. #define ERREXIT2(emeth,msg,p1,p2)    prgmerr(0, msg, p1, p2)
  635. #define ERREXIT3(emeth,msg,p1,p2,p3)    prgmerr(0, msg, p1, p2, p3)
  636. #define ERREXIT4(emeth,msg,p1,p2,p3,p4)    prgmerr(0, msg, p1, p2, p3, p4)
  637.  
  638. #define MAKESTMT(stuff)        do { stuff } while (0)
  639.  
  640. /* Nonfatal errors (we'll keep going, but the data is probably corrupt) */
  641. /* Note that warning count is incremented as a side-effect! */
  642. #define WARNMS(emeth,msg)    \
  643.   MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
  644.         (emeth)->more_warning_level : (emeth)->first_warning_level)){ \
  645.         (*(emeth)->trace_message) (msg); } )
  646. #define WARNMS1(emeth,msg,p1)    \
  647.   MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
  648.         (emeth)->more_warning_level : (emeth)->first_warning_level)){ \
  649.         (emeth)->message_parm[0] = (p1); \
  650.         (*(emeth)->trace_message) (msg); } )
  651. #define WARNMS2(emeth,msg,p1,p2)    \
  652.   MAKESTMT( if ((emeth)->trace_level >= ((emeth)->num_warnings++ ? \
  653.         (emeth)->more_warning_level : (emeth)->first_warning_level)){ \
  654.         (emeth)->message_parm[0] = (p1); \
  655.         (emeth)->message_parm[1] = (p2); \
  656.         (*(emeth)->trace_message) (msg); } )
  657.  
  658. /* Informational/debugging messages */
  659. #define TRACEMS(emeth,lvl,msg)    \
  660.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  661.         (*(emeth)->trace_message) (msg); } )
  662. #define TRACEMS1(emeth,lvl,msg,p1)    \
  663.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  664.         (emeth)->message_parm[0] = (p1); \
  665.         (*(emeth)->trace_message) (msg); } )
  666. #define TRACEMS2(emeth,lvl,msg,p1,p2)    \
  667.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  668.         (emeth)->message_parm[0] = (p1); \
  669.         (emeth)->message_parm[1] = (p2); \
  670.         (*(emeth)->trace_message) (msg); } )
  671. #define TRACEMS3(emeth,lvl,msg,p1,p2,p3)    \
  672.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  673.         int * _mp = (emeth)->message_parm; \
  674.         *_mp++ = (p1); *_mp++ = (p2); *_mp = (p3); \
  675.         (*(emeth)->trace_message) (msg); } )
  676. #define TRACEMS4(emeth,lvl,msg,p1,p2,p3,p4)    \
  677.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  678.         int * _mp = (emeth)->message_parm; \
  679.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp = (p4); \
  680.         (*(emeth)->trace_message) (msg); } )
  681. #define TRACEMS8(emeth,lvl,msg,p1,p2,p3,p4,p5,p6,p7,p8)    \
  682.   MAKESTMT( if ((emeth)->trace_level >= (lvl)) { \
  683.         int * _mp = (emeth)->message_parm; \
  684.         *_mp++ = (p1); *_mp++ = (p2); *_mp++ = (p3); *_mp++ = (p4); \
  685.         *_mp++ = (p5); *_mp++ = (p6); *_mp++ = (p7); *_mp = (p8); \
  686.         (*(emeth)->trace_message) (msg); } )
  687.  
  688.  
  689. /* Methods used during JPEG compression. */
  690.  
  691. struct Compress_methods_struct {
  692.     /* Hook for user interface to get control after input_init */
  693.     METHOD(void, c_ui_method_selection, (compress_info_ptr cinfo));
  694.     /* Hook for user interface to do progress monitoring */
  695.     METHOD(void, progress_monitor, (compress_info_ptr cinfo,
  696.                     long loopcounter, long looplimit));
  697.     /* Input image reading & conversion to standard form */
  698.     METHOD(void, input_init, (compress_info_ptr cinfo));
  699.     METHOD(void, get_input_row, (compress_info_ptr cinfo,
  700.                      JSAMPARRAY pixel_row));
  701.     METHOD(void, input_term, (compress_info_ptr cinfo, int term));
  702.     /* Color space and gamma conversion */
  703.     METHOD(void, colorin_init, (compress_info_ptr cinfo));
  704.     METHOD(void, get_sample_rows, (compress_info_ptr cinfo,
  705.                        int rows_to_read,
  706.                        JSAMPIMAGE image_data));
  707.     METHOD(void, colorin_term, (compress_info_ptr cinfo));
  708.     /* Expand picture data at edges */
  709.     METHOD(void, edge_expand, (compress_info_ptr cinfo,
  710.                    long input_cols, int input_rows,
  711.                    long output_cols, int output_rows,
  712.                    JSAMPIMAGE image_data));
  713.     /* Downsample pixel values of a single component */
  714.     /* There can be a different downsample method for each component */
  715.     METHOD(void, downsample_init, (compress_info_ptr cinfo));
  716.     downsample_ptr downsample[MAX_COMPS_IN_SCAN];
  717.     METHOD(void, downsample_term, (compress_info_ptr cinfo));
  718.     /* Extract samples in MCU order, process & hand off to output_method */
  719.     /* The input is always exactly N MCU rows worth of data */
  720.     METHOD(void, extract_init, (compress_info_ptr cinfo));
  721.     METHOD(void, extract_MCUs, (compress_info_ptr cinfo,
  722.                     JSAMPIMAGE image_data,
  723.                     int num_mcu_rows,
  724.                     MCU_output_method_ptr output_method));
  725.     METHOD(void, extract_term, (compress_info_ptr cinfo));
  726.     /* Entropy encoding parameter optimization */
  727.     METHOD(void, entropy_optimize, (compress_info_ptr cinfo,
  728.                     MCU_output_caller_ptr source_method));
  729.     /* Entropy encoding */
  730.     METHOD(void, entropy_encode_init, (compress_info_ptr cinfo));
  731.     METHOD(void, entropy_encode, (compress_info_ptr cinfo,
  732.                       JBLOCK *MCU_data));
  733.     METHOD(void, entropy_encode_term, (compress_info_ptr cinfo));
  734.     /* JPEG file header construction */
  735.     METHOD(void, write_file_header, (compress_info_ptr cinfo));
  736.     METHOD(void, write_scan_header, (compress_info_ptr cinfo));
  737.     METHOD(void, write_jpeg_data, (compress_info_ptr cinfo,
  738.                        char *dataptr,
  739.                        int datacount));
  740.     METHOD(void, write_scan_trailer, (compress_info_ptr cinfo));
  741.     METHOD(void, write_file_trailer, (compress_info_ptr cinfo));
  742.     /* Pipeline control */
  743.     METHOD(void, c_pipeline_controller, (compress_info_ptr cinfo));
  744.     METHOD(void, entropy_output, (compress_info_ptr cinfo,
  745.                       char *dataptr,
  746.                       int datacount));
  747.     /* Overall control */
  748.     METHOD(void, c_per_scan_method_selection, (compress_info_ptr cinfo));
  749. };
  750.  
  751. /* Methods used during JPEG decompression. */
  752.  
  753. struct Decompress_methods_struct {
  754.     /* Hook for user interface to get control after reading file header */
  755.     METHOD(void, d_ui_method_selection, (decompress_info_ptr cinfo));
  756.     /* Hook for user interface to do progress monitoring */
  757.     METHOD(void, progress_monitor, (decompress_info_ptr cinfo,
  758.                     long loopcounter, long looplimit));
  759.     /* JPEG file scanning */
  760.     METHOD(int, read_file_header, (decompress_info_ptr cinfo));
  761.     METHOD(boolean, read_scan_header, (decompress_info_ptr cinfo));
  762.     METHOD(int, read_jpeg_data, (decompress_info_ptr cinfo));
  763.     METHOD(void, resync_to_restart, (decompress_info_ptr cinfo,
  764.                      int marker));
  765.     METHOD(void, read_scan_trailer, (decompress_info_ptr cinfo));
  766.     METHOD(void, read_file_trailer, (decompress_info_ptr cinfo));
  767.     /* Entropy decoding */
  768.     METHOD(void, entropy_decode_init, (decompress_info_ptr cinfo));
  769.     METHOD(void, entropy_decode, (decompress_info_ptr cinfo,
  770.                       JBLOCKROW *MCU_data));
  771.     METHOD(void, entropy_decode_term, (decompress_info_ptr cinfo));
  772.     /* MCU disassembly: fetch MCUs from entropy_decode, build coef array */
  773.     /* The reverse_DCT step is in the same module for symmetry reasons */
  774.     METHOD(void, disassemble_init, (decompress_info_ptr cinfo));
  775.     METHOD(void, disassemble_MCU, (decompress_info_ptr cinfo,
  776.                        JBLOCKIMAGE image_data));
  777.     METHOD(void, reverse_DCT, (decompress_info_ptr cinfo,
  778.                    JBLOCKIMAGE coeff_data,
  779.                    JSAMPIMAGE output_data, int start_row));
  780.     METHOD(void, disassemble_term, (decompress_info_ptr cinfo));
  781.     /* Cross-block smoothing */
  782.     METHOD(void, smooth_coefficients, (decompress_info_ptr cinfo,
  783.                        jpeg_component_info *compptr,
  784.                        JBLOCKROW above,
  785.                        JBLOCKROW currow,
  786.                        JBLOCKROW below,
  787.                        JBLOCKROW output));
  788.     /* Upsample pixel values of a single component */
  789.     /* There can be a different upsample method for each component */
  790.     METHOD(void, upsample_init, (decompress_info_ptr cinfo));
  791.     upsample_ptr upsample[MAX_COMPS_IN_SCAN];
  792.     METHOD(void, upsample_term, (decompress_info_ptr cinfo));
  793.     /* Color space and gamma conversion */
  794.     METHOD(void, colorout_init, (decompress_info_ptr cinfo));
  795.     METHOD(void, color_convert, (decompress_info_ptr cinfo,
  796.                      int num_rows, long num_cols,
  797.                      JSAMPIMAGE input_data,
  798.                      JSAMPIMAGE output_data));
  799.     METHOD(void, colorout_term, (decompress_info_ptr cinfo));
  800.     /* Color quantization */
  801.     METHOD(void, color_quant_init, (decompress_info_ptr cinfo));
  802.     METHOD(void, color_quantize, (decompress_info_ptr cinfo,
  803.                       int num_rows,
  804.                       JSAMPIMAGE input_data,
  805.                       JSAMPARRAY output_data));
  806.     METHOD(void, color_quant_prescan, (decompress_info_ptr cinfo,
  807.                        int num_rows,
  808.                        JSAMPIMAGE image_data,
  809.                        JSAMPARRAY workspace));
  810.     METHOD(void, color_quant_doit, (decompress_info_ptr cinfo,
  811.                     quantize_caller_ptr source_method));
  812.     METHOD(void, color_quant_term, (decompress_info_ptr cinfo));
  813.     /* Output image writing */
  814.     METHOD(void, output_init, (decompress_info_ptr cinfo));
  815.     METHOD(void, put_color_map, (decompress_info_ptr cinfo,
  816.                      int num_colors, JSAMPARRAY colormap));
  817.     METHOD(void, put_pixel_rows, (decompress_info_ptr cinfo,
  818.                       int num_rows,
  819.                       JSAMPIMAGE pixel_data));
  820.     METHOD(void, output_term, (decompress_info_ptr cinfo));
  821.     /* Pipeline control */
  822.     METHOD(void, d_pipeline_controller, (decompress_info_ptr cinfo));
  823.     /* Overall control */
  824.     METHOD(void, d_per_scan_method_selection, (decompress_info_ptr cinfo));
  825. };
  826.  
  827.  
  828. /* External declarations for routines that aren't called via method ptrs. */
  829. /* Note: use "j" as first char of names to minimize namespace pollution. */
  830. /* The PP macro hides prototype parameters from compilers that can't cope. */
  831.  
  832. #ifdef PROTO
  833. #define PP(arglist)    arglist
  834. #else
  835. #define PP(arglist)    ()
  836. #endif
  837.  
  838.  
  839. /* main entry for compression */
  840. EXTERN void jpeg_compress PP((compress_info_ptr cinfo));
  841.  
  842. /* default parameter setup for compression */
  843. EXTERN void j_c_defaults PP((compress_info_ptr cinfo, int quality,
  844.                  boolean force_baseline));
  845. EXTERN void j_monochrome_default PP((compress_info_ptr cinfo));
  846. EXTERN void j_set_quality PP((compress_info_ptr cinfo, int quality,
  847.                   boolean force_baseline));
  848. /* advanced compression parameter setup aids */
  849. EXTERN void j_add_quant_table PP((compress_info_ptr cinfo, int which_tbl,
  850.                   const QUANT_VAL *basic_table,
  851.                   int scale_factor, boolean force_baseline));
  852. EXTERN int j_quality_scaling PP((int quality));
  853.  
  854. /* main entry for decompression */
  855. EXTERN int jpeg_decompress PP((decompress_info_ptr cinfo));
  856.  
  857. /* default parameter setup for decompression */
  858. EXTERN void j_d_defaults PP((decompress_info_ptr cinfo,
  859.                  boolean standard_buffering));
  860.  
  861. /* forward DCT */
  862. EXTERN void j_fwd_dct PP((DCTBLOCK data));
  863. /* inverse DCT */
  864. EXTERN void j_rev_dct PP((DCTBLOCK data));
  865.  
  866. /* utility routines in jutils.c */
  867. EXTERN long jround_up PP((long a, long b));
  868. EXTERN void jcopy_sample_rows PP((JSAMPARRAY input_array, int source_row,
  869.                   JSAMPARRAY output_array, int dest_row,
  870.                   int num_rows, long num_cols));
  871. EXTERN void jcopy_block_row PP((JBLOCKROW input_row, JBLOCKROW output_row,
  872.                 long num_blocks));
  873. EXTERN void jzero_far PP((void FAR * target, size_t bytestozero));
  874.  
  875. /* method selection routines for compression modules */
  876. EXTERN void jselcpipeline PP((compress_info_ptr cinfo)); /* jcpipe.c */
  877. EXTERN void jselchuffman PP((compress_info_ptr cinfo)); /* jchuff.c */
  878. EXTERN void jselcarithmetic PP((compress_info_ptr cinfo)); /* jcarith.c */
  879. EXTERN void jselexpand PP((compress_info_ptr cinfo)); /* jcexpand.c */
  880. EXTERN void jseldownsample PP((compress_info_ptr cinfo)); /* jcsample.c */
  881. EXTERN void jselcmcu PP((compress_info_ptr cinfo)); /* jcmcu.c */
  882. EXTERN void jselccolor PP((compress_info_ptr cinfo));    /* jccolor.c */
  883. /* The user interface should call one of these to select input format: */
  884. EXTERN void jselrgif PP((compress_info_ptr cinfo)); /* jrdgif.c */
  885. EXTERN void jselrppm PP((compress_info_ptr cinfo)); /* jrdppm.c */
  886. EXTERN void jselrrle PP((compress_info_ptr cinfo)); /* jrdrle.c */
  887. EXTERN void jselrtarga PP((compress_info_ptr cinfo)); /* jrdtarga.c */
  888. /* and one of these to select output header format: */
  889. EXTERN void jselwjfif PP((compress_info_ptr cinfo)); /* jwrjfif.c */
  890.  
  891. /* method selection routines for decompression modules */
  892. EXTERN void jseldpipeline PP((decompress_info_ptr cinfo)); /* jdpipe.c */
  893. EXTERN void jseldhuffman PP((decompress_info_ptr cinfo)); /* jdhuff.c */
  894. EXTERN void jseldarithmetic PP((decompress_info_ptr cinfo)); /* jdarith.c */
  895. EXTERN void jseldmcu PP((decompress_info_ptr cinfo)); /* jdmcu.c */
  896. EXTERN void jselbsmooth PP((decompress_info_ptr cinfo)); /* jbsmooth.c */
  897. EXTERN void jselupsample PP((decompress_info_ptr cinfo)); /* jdsample.c */
  898. EXTERN void jseldcolor PP((decompress_info_ptr cinfo));    /* jdcolor.c */
  899. EXTERN void jsel1quantize PP((decompress_info_ptr cinfo)); /* jquant1.c */
  900. EXTERN void jsel2quantize PP((decompress_info_ptr cinfo)); /* jquant2.c */
  901. /* The user interface should call one of these to select input format: */
  902. EXTERN void jselrjfif PP((decompress_info_ptr cinfo)); /* jrdjfif.c */
  903. /* and one of these to select output image format: */
  904. EXTERN void jselwgif PP((decompress_info_ptr cinfo)); /* jwrgif.c */
  905. EXTERN void jselwppm PP((decompress_info_ptr cinfo)); /* jwrppm.c */
  906. EXTERN void jselwrle PP((decompress_info_ptr cinfo)); /* jwrrle.c */
  907. EXTERN void jselwtarga PP((decompress_info_ptr cinfo)); /* jwrtarga.c */
  908.  
  909. /* method selection routines for system-dependent modules */
  910. EXTERN void jselerror PP((external_methods_ptr emethods)); /* jerror.c */
  911. EXTERN void jselmemmgr PP((external_methods_ptr emethods)); /* jmemmgr.c */
  912.  
  913.  
  914. /* We assume that right shift corresponds to signed division by 2 with
  915.  * rounding towards minus infinity.  This is correct for typical "arithmetic
  916.  * shift" instructions that shift in copies of the sign bit.  But some
  917.  * C compilers implement >> with an unsigned shift.  For these machines you
  918.  * must define RIGHT_SHIFT_IS_UNSIGNED.
  919.  * RIGHT_SHIFT provides a proper signed right shift of an INT32 quantity.
  920.  * It is only applied with constant shift counts.  SHIFT_TEMPS must be
  921.  * included in the variables of any routine using RIGHT_SHIFT.
  922.  */
  923.  
  924. #ifdef RIGHT_SHIFT_IS_UNSIGNED
  925. #define SHIFT_TEMPS    INT32 shift_temp;
  926. #define RIGHT_SHIFT(x,shft)  \
  927.     ((shift_temp = (x)) < 0 ? \
  928.      (shift_temp >> (shft)) | ((~((INT32) 0)) << (32-(shft))) : \
  929.      (shift_temp >> (shft)))
  930. #else
  931. #define SHIFT_TEMPS
  932. #define RIGHT_SHIFT(x,shft)    ((x) >> (shft))
  933. #endif
  934.  
  935.  
  936. /* Miscellaneous useful macros */
  937.  
  938. #undef MAX
  939. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  940. #undef MIN
  941. #define MIN(a,b)    ((a) < (b) ? (a) : (b))
  942.  
  943.  
  944. #define RST0    0xD0        /* RST0 marker code */
  945.